/** 字典控件 * */
document.write('<script type="text/javascript" src="' + context
		+ '/system/wordbook/js/wbstore.js"></script>');
/** 表数据处理控件 * */
document.write('<script type="text/javascript" src="' + context
		+ '/system/tablequery/js/tablehandler.js"></script>');
/** 对象选择列表控件 * */
document.write('<script type="text/javascript" src="' + context
		+ '/system/objquery/js/querylist4extN.js"></script>');

function receiveGZObject(record) {
	Ext.getCmp('topForm').form.findField('GZNAME').setValue(record
			.get('GZNAME'));
	Ext.getCmp('topForm').form.findField('TYPENAME').setValue(record
			.get('TYPENAME'));
	Ext.getCmp('topForm').form.findField('GZCODE').setValue(record
			.get('GZCODE'));
	Ext.getCmp('topForm').form.findField('GZFAC')
			.setValue(record.get('GZFROM'));
}

function initPage(id) {
	initOQExtRegionList('north', 'allgzlist4ly', initPagePanel, 10);

}

function initPagePanel(grid, cm, sm) {
	grid.setHeight(250);

	sm.on('rowselect', function(sm_, rowIndex, record) {
				selectLyList(record, true);
			});

	/*
	 * sm.on('rowdeselect', function(sm_, rowIndex, record) {
	 * selectLyList(record, false); });
	 */

	/*
	 * grid.on('click', function(e) { var t = e.getTarget(); var v = grid.view;
	 * var rowIdx = v.findRowIndex(t); var record =
	 * grid.getStore().getAt(rowIdx);
	 * 
	 * if (record) { selectLyList(record,
	 * grid.getSelectionModel().isSelected(rowIdx)); } });
	 */

	var gzly = getWBComboStore('gzlyyt', '', 'YT_', '用途', 'YT', null, '', false);

	gzly.anchor = '98%';

	var topPanel = new Ext.FormPanel({
		id : 'topForm',
		title : '<font class="oaFont">资产领用登记</font>',
		// labelAlign : 'top',
		region : 'center',
		frame : true,
		height : 200,
		bodyStyle : 'padding:5px 5px 0',
		border : false,
		autoScroll : true,
		items : [{
			layout : 'column',
			items : [{
				columnWidth : .5,
				layout : 'form',
				items : [{
							xtype : 'textfield',
							fieldLabel : '领用人',
							name : 'SYUSERNAME',
							readOnly : true,
							value : userName,
							anchor : '98%'
						}, {
							xtype : 'hidden',
							fieldLabel : '领用人ID',
							name : 'SYUSERID',
							value : userId
						}, {
							xtype : 'textfield',
							fieldLabel : '所在部门',
							name : 'SYGROUPNAME',
							readOnly : true,
							value : groupName,
							anchor : '98%'
						}, {
							xtype : 'hidden',
							fieldLabel : '所在部门ID',
							name : 'SYGROUPID',
							value : groupId
						}, gzly, {
							xtype : 'hidden',
							id : 'YT',
							name : 'YT'
						}, {
							xtype : 'hidden',
							name : 'LYSTATUS',
							value : '0'
						}, {
							xtype : 'hidden',
							name : 'SQDATE',
							value : ''
						}, {
							xtype : 'hidden',
							name : 'GZID',
							value : ''
						}, {
							xtype : 'hidden',
							name : 'GZNAME',
							value : ''
						}, {
							xtype : 'hidden',
							name : 'TYPENAME',
							value : ''
						}, {
							xtype : 'hidden',
							name : 'TYPECODE',
							value : ''
						}, {
							xtype : 'hidden',
							name : 'GZCOUNT',
							value : ''
						}, {
							xtype : 'hidden',
							name : 'LYLABEL',
							value : ''
						}, {
							xtype : 'textarea',
							name : 'LYDESC',
							fieldLabel : '备注说明',
							height : 100,
							anchor : '98%'
						}],
				buttons : [{
					text : '<font class="oaFont">提 交</font>',
					handler : function() {
						if (topPanel.getForm().isValid()) {
							saveTable4Ext('APP_GDZC_LINGYONG,APP_GDZC_LY',
									'single,multi', '-1,countLY', function() {
										if (topPanel.getForm().isValid()) {
											topPanel.form.findField('SQDATE')
													.setValue(nowTime());

											var items = A('item', 'lyitem');

											/* 领用明细计数器 */
											var countLY = 0;
											var countLYStr = '';
											var labels = '';

											if (items.length > 0) {
												for (var i = 0; i < items.length; i++) {
													var _idx = items[i]
															.getAttribute('idx');

													if (isblank($('gcount_'
															+ _idx))
															|| !isnumber($('gcount_'
																	+ _idx))
															|| $('gcount_'
																	+ _idx).value < 1) {
														alert('请填写正确的领用数量');
														return false;
													}

													if (i == 0) {
														labels = '{"gzid":"'
																+ items[i]
																		.getAttribute('gzid')
																+ '","gzname":"'
																+ items[i]
																		.getAttribute('gzname')
																+ '","tname":"'
																+ items[i]
																		.getAttribute('tname')
																+ '","tcode":"'
																+ items[i]
																		.getAttribute('tcode')
																+ '","gcount":"'
																+ $('gcount_'
																		+ _idx).value
																+ '"}';
													} else {
														labels += ',{"gzid":"'
																+ items[i]
																		.getAttribute('gzid')
																+ '","gzname":"'
																+ items[i]
																		.getAttribute('gzname')
																+ '","tname":"'
																+ items[i]
																		.getAttribute('tname')
																+ '","tcode":"'
																+ items[i]
																		.getAttribute('tcode')
																+ '","gcount":"'
																+ $('gcount_'
																		+ _idx).value
																+ '"}';
													}

													var gcount = parseInt($('gcount_'
															+ _idx).value);

													for (var j = 0; j < gcount; j++) {
														countLYStr += countLY
																+ ',';

														topPanel.add({
															xtype : 'hidden',
															name : 'YT_'
																	+ countLY,
															value : topPanel.form
																	.findField('YT')
																	.getValue()
														});

														topPanel.add({
																	xtype : 'hidden',
																	name : 'SYUSERID_'
																			+ countLY,
																	value : userId
																});

														topPanel.add({
															xtype : 'hidden',
															name : 'SYUSERNAME_'
																	+ countLY,
															value : userName
														});

														topPanel.add({
																	xtype : 'hidden',
																	name : 'SYGROUPID_'
																			+ countLY,
																	value : groupId
																});

														topPanel.add({
															xtype : 'hidden',
															name : 'SYGROUPNAME_'
																	+ countLY,
															value : groupName
														});

														topPanel.add({
															xtype : 'hidden',
															name : 'GZID_'
																	+ countLY,
															value : items[i]
																	.getAttribute('gzid')
														});

														topPanel.add({
															xtype : 'hidden',
															name : 'GZNAME_'
																	+ countLY,
															value : items[i]
																	.getAttribute('gzname')
														});

														topPanel.add({
															xtype : 'hidden',
															name : 'TYPENAME_'
																	+ countLY,
															value : items[i]
																	.getAttribute('tname')
														});

														topPanel.add({
															xtype : 'hidden',
															name : 'TYPECODE_'
																	+ countLY,
															value : items[i]
																	.getAttribute('tcode')
														});

														topPanel.add({
																	xtype : 'hidden',
																	name : 'LYSTATUS_'
																			+ countLY,
																	value : '0'
																});
														
														topPanel.add({
																	xtype : 'hidden',
																	name : 'LYCOUNT_'
																			+ countLY,
																	value : '1'
																});		

														countLY++;
													}
												}

												topPanel.form
														.findField('LYLABEL')
														.setValue('[' + labels
																+ ']');

												topPanel.add({
															xtype : 'hidden',
															name : 'countLY',
															value : countLYStr
														});

												topPanel.doLayout();
											} else {
												Ext.Msg.alert('提示', '请选择待领用资产');

												return false;
											}

											return true;
										}
										return false;
									}, function() {
										alert('提交保存成功');

										/*document.location = context
												+ '/system/tablequery/commonlist.jsp?queryKey=mylylist';*/
										handleRefresh();
									}, topPanel, null, 'lingyongdj');
						}
					}
				}, {
					text : '<font class="oaFont">重 置</font>',
					handler : function() {
						top.getForm().reset();
					}
				}]
			}, {
				columnWidth : .5,
				layout : 'column',
				border : false,
				autoScroll : false,
				title : '<font class="oaFont">已选择待领用资产列表</font>',
				html : '<div id="lyobjarea" style="width:100%;height:100%;padding:5px;"><font class="oaFont" color="red">请从上面列表中选择待领用的资产</font></div>'
			}]
		}]
	});

	var viewport = new Ext.Viewport({
				layout : 'fit',
				items : [new Ext.Panel({
							autoScroll : false,
							layout : 'border',
							border : false,
							layoutConfig : {
								titleCollapse : true,
								animate : true,
								activeOnTop : false
							},

							items : [grid, topPanel]
						})],

				listeners : {
					render : function() {

					}
				}
			});
}

var idx = 0;
/**
 * 选择资产领用信息
 * 
 * @param {}
 *            record 选择资产record
 * @param {}
 *            selected 是否选定
 */
function selectLyList(record, selected) {
	var id = record.data.GZID;

	if (selected && !$('gz_' + id)) {
		var str = '<table style="margin:2px;" width="95%" border="0" item="lyitem" idx="'
				+ idx
				+ '" gzid="'
				+ record.data.GZID
				+ '" gzname="'
				+ record.data.GZNAME
				+ '" tname="'
				+ record.data.TYPENAME
				+ '" tcode="'
				+ record.data.GZCODE1
				+ '" id="gz_'
				+ id
				+ '"><tr><td width="7" align="center"><img style="cursor:hand;" title="删除" src="'
				+ context
				+ '/app/gdzc/images/del.gif" onclick="javascrpt:deldiv(\''
				+ id
				+ '\')"></td><td width="56%"><font class="oaFont">('
				+ record.data.TYPENAME
				+ ')&nbsp;'
				+ record.data.GZNAME
				+ '</font></td><td width="40%" align="left"><font class="oaFont" style="font-weight:bold;">资产编号</font> '
				+ record.data.GZCODE1
				+ '<div style="display:none;"><font class="oaFont" style="font-weight:bold;">领用数量</font> <input type="text" class="x-form-text x-form-field" id="gcount_'
				+ idx + '" value="1"></div></td></tr></table>';

		if (A('item', 'lyitem').length == 0) {
			$('lyobjarea').innerHTML = str;
		} else {
			$('lyobjarea').innerHTML += str;
		}

		idx++;
	} else if (!selected && $('gz_' + id)) {
		deldiv(id);
	}
}

function deldiv(id) {
	if ($('gz_' + id)) {
		$('gz_' + id).parentNode.removeChild($('gz_' + id));
		initDivLabel();
	}
}

function initDivLabel() {
	if ($('lyobjarea').innerHTML == '') {
		$('lyobjarea').innerHTML = '<font class="oaFont" color="red">请从上面列表中选择待领用的资产</font>';
	}
}